#!/bin/bash
echo "请问，您想要做什么？这里是php通用开发环境。输入 exit 退出。"
echo "  安装docker：install docker"
echo "  卸载docker：remove docker"
echo "  安装docker-compose：install compose"
echo "  卸载docker-compose：remove compose"
echo "  启动PHP环境：build php env"
echo "  停止PHP环境：stop php env"
echo "  卸载PHP环境：remove php env"
echo "  查看容器列表：docker ps"
echo "  查看镜像列表：docker images"
echo "  安装portainer：install portainer"
echo "  卸载portainer：remove portainer"
echo "  进入hyperf容器：enter hyperf"
echo "  安装mineadmin项目后端：install mineadmin"
echo "  安装mineadmin项目前端：install mineadmin-vue"
echo ""

# 打包：tar -zcvf src.tar.gz src

function go(){
  if [ "$install_type" ]
  then
    if [ "$install_type" == "install docker" ]
    then
      sudo apt-get update
      # 先安装最新版
      curl -sSL https://get.daocloud.io/docker | sh
      # 再降级安装20.10.23版本（debian deepin）
      sudo apt-get install docker-ce=5:20.10.23~3-0~debian-buster docker-ce-cli=5:20.10.23~3-0~debian-buster containerd.io
      # 再降级安装20.10.23版本（ubuntu）
      sudo apt-get install docker-ce=5:20.10.23~3-0~ubuntu-jammy docker-ce-cli=5:20.10.23~3-0~ubuntu-jammy containerd.io

		# 将docker卸载干净
    elif [ "$install_type" == "remove docker" ]
    then
      sudo apt-get autoremove docker docker-ce docker-engine  docker.io  containerd runc -y
      dpkg -l | grep docker
      dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P # 删除无用的相关的配置文件
      sudo apt-get autoremove docker-ce-* -y
      sudo apt-get remove docker docker-engine docker.io containerd runc
      sudo apt-get purge docker-ce
      ps -ef|grep docker|grep -v grep|xargs kill -s 9
      sudo apt-get remove docker-*
      sudo apt-get remove docker-ce-cli-*
      sudo apt-get remove docker-scan-plugin*
      sudo rm -rf /etc/systemd/system/docker.service.d
      sudo rm -rf /var/lib/docker
      sudo rm -rf /var/lib/containerd
      sudo rm -rf /etc/docker
      sudo rm -rf /run/docker
      sudo rm -rf /var/lib/dockershim


    elif [ "$install_type" == "install compose" ]
    then
      curl -L https://get.daocloud.io/docker/compose/releases/download/v2.15.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
      sudo chmod +x /usr/local/bin/docker-compose
      sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
      docker-compose version
    elif [ "$install_type" == "remove compose" ]
    then
      rm -rf /usr/local/bin/docker-compose
      rm -rf /usr/bin/docker-compose

    #构建php环境
    elif [ "$install_type" == "build php env" ]
    then

      sudo docker-compose up -d

    elif [ "$install_type" == "stop php env" ]
    then
      sudo docker-compose stop

    elif [ "$install_type" == "remove php env" ]
    then
      sudo docker-compose down

    elif [ "$install_type" == "docker ps" ]
    then
      sudo docker ps

    elif [ "$install_type" == "docker images" ]
    then
      sudo docker images

    elif [ "$install_type" == "install portainer" ]
    then
      sudo docker run -d -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock portainer/portainer-ce

    elif [ "$install_type" == "remove portainer" ]
    then
      sudo docker rm -f portainer

    elif [ "$install_type" == "enter hyperf" ]
    then
      sudo docker exec -it common_hyperf bash

    # 安装mineadmin项目后端
    elif [ "$install_type" == "install mineadmin" ]
    then
      # 后端
      sudo docker exec -it common_hyperf bash -c "git clone https://gitee.com/mineadmin/mineadmin.git /www/mineadmin && cd /www/mineadmin && composer install && php bin/hyperf.php mine:install && php bin/hyperf.php mine:install && php watch -c"

   # 安装mineadmin项目前端
    elif [ "$install_type" == "install mineadmin-vue" ]
    then
      sudo docker exec -it common_hyperf bash -c "git clone https://gitee.com/mineadmin/mineadmin-vue.git /www/mineadmin-vue && apk add yarn && cd /www/mineadmin-vue && yarn install && yarn dev"

    else
			echo "请输入正确的值"
    fi
  else
    echo "请输入一个值"
  fi
}

# 死循环，这样可以多次处理
while [ true ]
do
	read -p "请输入一个值：" install_type
	if [ "$install_type" ] && [ "$install_type" == "exit" ]
	then
		break
	fi
	go $install_type
done
